package com.tarena.lbs.user.service.Impl;

import com.tarena.lbs.base.protocol.exception.BusinessException;
import com.tarena.lbs.common.encoder.JwtEncoder;
import com.tarena.lbs.common.enums.Roles;
import com.tarena.lbs.common.principle.UserPrinciple;
import com.tarena.lbs.user.dao.repository.AdminRepository;
import com.tarena.lbs.user.dao.repository.UserRepository;
import com.tarena.lbs.user.pojo.param.AdminLoginParam;
import com.tarena.lbs.user.pojo.param.UserLoginParam;
import com.tarena.lbs.user.pojo.po.AdminPO;
import com.tarena.lbs.user.pojo.po.UserPO;
import com.tarena.lbs.user.pojo.vo.LoginVO;
import com.tarena.lbs.user.service.LoginService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


@Slf4j
@Service
public class LoginServiceImpl implements LoginService {

    @Autowired
    private AdminRepository adminRepository;
    @Autowired
    private UserRepository  userRepository;
    @Autowired
    private JwtEncoder<UserPrinciple> jwtEncoder;
    @Override
    public LoginVO adminLogin(AdminLoginParam param) throws BusinessException {
        log.debug("管理员登录",param);
        AdminPO adminPO = adminRepository.getAdminByPhone(param.getPhone());
        if (adminPO==null){
            throw new BusinessException("-2","管理员不存在");
        }
        if (!adminPO.getAccountPassword().equals(param.getPassword())){
            throw new BusinessException("-3","密码错误");
        }
        String jwt = generateJwt(adminPO);
        LoginVO LoginVO = new LoginVO();
        LoginVO.setId(adminPO.getId());
        LoginVO.setJwt(jwt);
        LoginVO.setNickname(adminPO.getNickname());
        log .debug("管理员登录成功",adminPO);
        return LoginVO;
    }

    @Override
    public LoginVO userLogin(UserLoginParam param) throws BusinessException {
        log.debug("用户登录",param);
        UserPO userPO = userRepository.getUserByPhone(param.getPhone());
        if (userPO==null){
            throw new BusinessException("-4","用户不存在");
        }
        if (userPO.getPassword().equals(param.getPassword())){
            throw new BusinessException("-3","密码错误");
        }
        String jwt = goodJwt(userPO);
        LoginVO LoginVO = new LoginVO();
        LoginVO.setId(userPO.getId());
        LoginVO.setJwt(jwt);
        LoginVO.setNickname(userPO.getUserName());
        log .debug("用户登录成功",userPO);
        return LoginVO;
    }

    private String goodJwt(UserPO userPO) throws BusinessException {
        UserPrinciple principle = new UserPrinciple();
        principle.setId(userPO.getId());
        principle.setNickname(userPO.getUserName());
        principle.setRole(Roles.USER);
        return jwtEncoder.generateToken(principle);
    }
    private String generateJwt(AdminPO adminPO) throws BusinessException {
        UserPrinciple principle = new UserPrinciple();
        principle.setId(adminPO.getId());
        principle.setNickname(adminPO.getNickname());
        principle.setRole(adminPO.getAccountType()==0? Roles.ADMIN:Roles.SHOP);
        return jwtEncoder.generateToken(principle);
    }

}
